.text
.align 3
.section .rodata
.LC0:
    .string "numbers.dat"
.LC1:
    .string "wb"
.LC2:
    .string "Could not open %s for output.\n"
.LC3:
    .string "rb"
.LC4:
    .string "Could not open %s for random access.\n"
.LC5:
    .string "Enter an index in the range 0-%d.\n"
.LC6:
    .string "%d"
.LC7:
    .string "The value there is %f.\n"
.LC8:
    .string "Next index (out of range to quit):"
.LC9:
    .string "Bye!"
.align 3
.LC10:
    .long	0
	.long	1079574528
.align 3
.LC11:
    .long	0
	.long	1072693248
// 这个程序暂时编译不过去，原因是riscv的指令立即数只有12位，也就是说，最大4096，超过这个数，指令存不下这个立即数了，得拆成两半来搞
// 暂时先留着，往后来弄
.text
.globl main
.type main, @function
main:
    addi sp, sp, -8048
    sd ra, 8040(sp)
    sd s0, 8032(sp)
    addi s0, sp, 8032

    // 分配局部变量
    // numbers: -8000(s0) /* numbers */
    // value: -8008(s0) /* value */
    // file: -8016(s0) /* file */
    // i: -8020(s0) /* i */
    // pos: -8024(s0) /* pos */
    // iofile: -8032(s0) /* iofile */

    lui a5, %hi(.LC0)
    addi a5, %lo(.LC0)
    sd a5, -8016(s0) /* file */
.FOR_14_INIT:
    sw zero, -8020(s0) /* i */
    j .FOR_14_COMPARE
.FOR_14_ENTER:
    lw a5, -8020(s0) /* i */
    addiw a5, a5, 1
    sext.w a5, a5
    mv a6, a5
    fcvt.d.w fa4, a5
    lui a5, %hi(.LC10)
    fld fa5, %lo(.LC10)(a5)
    fmul.d fa5, fa4, fa5

    fcvt.d.w fa4, a6
    lui a5, %hi(.LC11)
    fld fa3, %lo(.LC11)(a5)
    fdiv.d fa3, fa3, fa4
    fadd.d fa5, fa5, fa3

    addi a5, s0, -8000 /* offset of numbers */
    lw a6, -8020(s0) /* i */
    sext.w a6, a6
    slli a6, a6, 3
    add a5, a5, a6
    fsd fa5, 0(a5)
.FOR_14_INCR:
    lw a5, -8020(s0) /* i */
    addiw a5, a5, 1
    sw a5, -8020(s0) /* i */
.FOR_14_COMPARE:
    lw a5, -8020(s0) /* i */
    sext.w a5, a5
    li a6, 1000
    blt a5, a6, .FOR_14_ENTER
.FOR_14_END:
    ld a0, -8016(s0) /* file */
    lui a5, %hi(.LC1)
    addi a1, a5, %lo(.LC1)
    call fopen
    sd a0, -8032(s0) /* iofile */
    bne a0, zero, .OPEN_IO_FILE_SUCCESS
.OPEN_IO_FILE_FAILED:
    lui a5, %hi(stderr)
    ld a0, %lo(stderr)(a5)
    lui a5, %hi(.LC2)
    addi a1, %lo(.LC2)
    ld a2, -8016(s0) /* file */
    call fprintf
    // exit(1)
    li a0, 1
    call exit
.OPEN_IO_FILE_SUCCESS:
    addi a0, s0, -8000 /* offset of numbers */
    li a1, 8
    li a2, 1000
    ld a3, -8032(s0) /* iofile */
    call fwrite
    ld a0, -8032(s0) /* iofile */
    call fclose
    ld a0, -8016(s0) /* file */
    lui a5, %hi(.LC3)
    addi a1, a5, %lo(.LC3)
    call fopen
    sd a0, -8032(s0) /* iofile */
    bne a0, zero, .OPEN_RD_IO_FILE_SUCCESS
.OPEN_RD_IO_FILE_FAILED:
    lui a5, %hi(stderr)
    ld a0, %lo(stderr)(a5)
    lui a5, %hi(.LC4)
    addi a1, %lo(.LC4)
    ld a2, -8016(s0) /* file */
    call fprintf
    // exit(1)
    li a0, 1
    call exit
.OPEN_RD_IO_FILE_SUCCESS:
    lui a5, %hi(.LC5)
    addi a0, a5, %lo(.LC5)
    li a1, 1000
    call printf
.WHILE_33_INIT:
    j .WHILE_33_COMPARE
.WHILE_33_ENTER:
    lw a5, -8020(s0) /* i */
    sext.w a5, a5
    slli a5, a5, 3
    sw a5, -8024(s0) /* pos */
    ld a0, -8032(s0) /* iofile */
    lw a1, -8024(s0) /* pos */
    li a2, 0
    call fseek
    addi a0, s0, -8008 /* offset of value */
    li a1, 8
    li a2, 1
    ld a3, -8032(s0) /* iofile */
    call fread
    lui a5, %hi(.LC7)
    addi a0, a5, %lo(.LC7)
    fld fa5, -8008(s0) /* value */
    fmv.x.d a1, fa5
    call printf
    lui a5, %hi(.LC8)
    addi a0, a5, %lo(.LC8)
    call puts
.WHILE_33_INCR:
.WHILE_33_COMPARE:
    lui a5, %hi(.LC6)
    addi a0, a5, %lo(.LC6)
    addi a1, s0, -8020 /* offset of i */
    call scanf
    sext.w a5, a0
    li a6, 1
    bne a5, a6, .WHILE_33_END
    lw a5, -8020(s0) /* i */
    sext.w a5, a5
    blt a5, zero, .WHILE_33_END
    li a6, 1000
    bge a5, a6, .WHILE_33_END
    j .WHILE_33_ENTER
.WHILE_33_END:
    ld a0, -8032(s0) /* iofile */
    call fclose
    lui a5, %hi(.LC9)
    addi a0, %lo(.LC9)
    call puts

.EXIT_SUCCESS:
    mv a0, zero    
    ld ra, 8(s0)
    ld s0, 0(s0)
    addi sp, sp, 8048
    jr ra